package aaexcercise.chap5;

import access.Dinner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/3/16.
 */
public class TestDate {
    public static void main(String[] args) {
        String a = getYesterdayStr("sysdate-0", "M月d日");
        System.out.println(a);
    }
    /**
     *TODO(针对新彩信做的时间处理)
     *@param d 为兼容以前所有d的值为sysdate,或者sysdate-n这种形式
     *@param format
     *@return
     */
    public static String getYesterdayStr(String d,String format){
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat(format==null?"M月d日":format);
        int nn=-1;
        if(d!=null){
            d=d.toLowerCase();
            if(d.contains("-")||d.contains("+")){
                String n=null;
                if(d.contains("-")){
                    n=d.substring(d.indexOf("-")+1);
                }
                if(d.contains("+")){
                    n=d.substring(d.indexOf("+")+1);
                }
                System.out.println(n);

                nn=Integer.parseInt(n.trim());
                nn=d.contains("-")?-(nn+1):nn;
                System.out.println(nn);
            }
        }
        date=new Date(System.currentTimeMillis()+24*60*60*1000*nn);
        return sdf.format(date);
    }
    /**
     * 获得指定日期的前一天
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {//可以用new Date().toLocalString()传递参数
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
                .getTime());
//        Tank tank = new Tank(false);

        return dayBefore;
    }


}
